package org.argeproje.resim.proc.tools;

import org.argeproje.resim.proc.data.ConverterUT;

public class MathUT {
	public static int getSize(float[][] array){
		return array.length * array[0].length;
	}
	public static float getMaximum(float[][] array){
		float max = array[0][0];
		for (int i = 0; i < array.length; i++) {
			for (int j = 0; j < array[0].length; j++) {
				if(max < array[i][j]) max = array[i][j];
			}
		}
		return max;
	}
	public static double getSum(float[][] array){
		float total = 0;
		for (int i = 0; i < array.length; i++) {
			for (int j = 0; j < array[0].length; j++) {
				total = total + array[i][j];
			}
		}
		return total;
	}
	public static float getMean(float[][] array){
		float mean = (float) (getSum(array) / getSize(array));
		return mean;
	}
	public static float getNormalizedMean(float[][] array, float max){
		return getMean(array) / max;
	}
	public static double getVariance(float[][] array){
		double mean = getMean(array);
		double variance = 0.0F;
		for (float[] fs : array) {
			for (float f : fs) {
				variance = variance + Math.pow((f - mean), 2.0);
			}
		}
		return variance;
	}
	public static float getNormalizedVariance(float[][] array, float min, float max){
		double variance = getVariance(array);
		return (float) (variance / (float) Math.pow((max - min), 2.0) * 4.0 / (float)getSize(array));
	}
	public static float[] getIntegerSeries(int startValue, int count){
		float[] result = new float[count];
		for(int i = startValue; i < count + startValue; i++){
			result[i - startValue] = i;
		}
		return result;
	}
	public static float[][] getFFT(float[] data){
		int length = data.length;
		double[] realInput = ConverterUT.get1DDoubleArray(data);
		double[] imInput = ConverterUT.get1DDoubleArray(0.0, length);
		float[][] result = new float[2][length];
		double[] realOut = new double[length];
		double[] imOut = new double[length];
		/*
		Transform transform = new Transform();
	    transform.doIt(realInput, imInput, 1.0, realOut,
	    		imOut);
	    result[0] = ConverterUT.get1DFloatArray(realOut);
	    result[1] = ConverterUT.get1DFloatArray(imOut);*/
	    return result;
	}
	public static float[] getComplexMagnitude(float[][] complexArray){
		float[] result = new float[complexArray[0].length];
		for (int i = 0; i < result.length; i++) {
			result[i] = (float) Math.sqrt(Math.pow(complexArray[0][i], 2.0) + 
					Math.pow(complexArray[1][i], 2.0));
		}
		return result;
	}
}
